import axios from 'axios';
import { post, get } from '@/utils/request';

interface DirectoryRresponse {
  code: number;
  msg: string;
  status: boolean;
  data: any;
  fail: boolean;
}

interface DirectoryInfo {
  id: number;
  name: string;
  parentId: number;
  directoryPath: string;
  userId: number;
  createDateTime?:string;
}

/**
 * 添加目录
 * @returns 添加目录结果
 */
export const addDirectoryInfo = async (data: Partial<DirectoryInfo>): Promise<DirectoryRresponse> => {
  return post(`/engine/starhub/directory/addDirectoryInfo`,data);
};

/**
 * 更新目录
 * @returns 更新目录结果
 */
export const updateDirectoryInfo = async (data: Partial<DirectoryInfo>): Promise<DirectoryRresponse> => {
  return post('/engine/starhub/directory/updateDirectoryInfo',data);
};

/**
 * 获取目录信息
 * @returns 获取目录
 */
export const getDirectoryInfoInfo = async (id:number): Promise<DirectoryRresponse> => {
  return get(`/engine/starhub/directory/getDirectoryInfoInfo/${id}`);
};

/**
 * 删除目录信息，前端判断是否有下级目录
 * @returns 删除目录
 */
export const deleteDirectoryInfoRecord = async (data:{ids:string}): Promise<DirectoryRresponse> => {
	return post(`/engine/starhub/directory/deleteDirectoryInfoRecord`,data);
};

/**
 * 根据父级目录ID获取树形目录
 * @returns 获取目录
 */
export const getDirectoryInfoList = async (data:{parentId:number}): Promise<DirectoryRresponse> => {
  return post(`/engine/starhub/directory/getDirectoryInfoList`,data);
};
/**
 * 获取目录列表
 */
export const getDirectoryInfoNormalList = async (data:{parentId:number}): Promise<DirectoryRresponse> => {
  return post(`/engine/starhub/directory/getDirectoryInfoNormalList`,data);
};


/**
 * 根据父级目录ID获取树形目录
 */
export const getDirectoryTreeSelect = async (data:{parentId:number}): Promise<DirectoryRresponse> => {
  return post(`/engine/starhub/directory/getDirectoryTreeSelect`,data);
};

/**
 * 根据目录ID,获取目录权限用户
 */
export const getDirectoryShareList = async (data:{directoryId:number}): Promise<DirectoryRresponse> => {
  return post(`/engine/starhub/directoryShare/getDirectoryShareList`,data);
};

/**
 * 目录赋权
 */
export const grantDirectoryUserShare = async (data:{directoryId:number;userIds?:string;permissionIds?:string}): Promise<DirectoryRresponse> => {
  return post(`/engine/starhub/directoryShare/grantDirectoryUserShare`,data);
};

/**
 * 目录赋权
 */
export const delDirectoryUserShare = async (data:{directoryId:number;userIds?:string;permissionIds?:string}): Promise<DirectoryRresponse> => {
  return post(`/engine/starhub/directoryShare/delDirectoryUserShare`,data);
};

/**
 * 获取目录下的附件  不分页
 */
export const getDocumentInfoList = async (data:{directoryId:number}): Promise<DirectoryRresponse> => {
  return post(`/engine/starhub/document/getDocumentInfoList`,data);
};


/**
 * 获取用户列表
 */
export const getUserList = async (): Promise<DirectoryRresponse> => {
  return get('/engine/starhub/sysUser/getList');
};


/**
 * 获取目录下的附件 分页
 */

export const getDocumentListByDir = async (data:{directoryId:number,pageNo:number,pageSize:number}): Promise<DirectoryRresponse> => {
  return post(`/engine/starhub/document/getDocumentListByDir`,data);
};


/**
 * 文件下载
 * @returns 文件下载
 */
export const downloadFileById = async (id:number,fileName?:string) => {
	const token = localStorage.getItem('token');
	axios.get(`/engine/starhub/document/download/${id}`,{
			headers: {
				'Content-Type': 'application/x-www-form-urlencoded',
				'Authorization':`Bearer ${token}`
			},
			responseType: 'blob'
		}).then(data=>{
			const url = window.URL.createObjectURL(data.request.response);
			const elink = document.createElement('a');
			elink.href = url;
			elink.target = '_self'; // 当前也 target打开新页面
			elink.setAttribute('download', fileName?fileName:"下载文件");
			elink.style.display = 'none';
			document.body.appendChild(elink);
			elink.click();
			document.body.removeChild(elink);
		});
};
